//==============================================================================
/**
 * @file	cb_main.c
 * @brief	簡単な説明を書く
 * @author	goto
 * @date	2006.04.03(月)
 *
 * ここに色々な解説等を書いてもよい
 *
 */
//==============================================================================
#include "common.h"
#include "poketool/poke_tool.h"
#include "system/procsys.h"
#include "system/snd_tool.h"
#include "include/system/lib_pack.h"
#include "include/system/brightness.h"
#include "include/system/heapdefine.h"
#include "include/system/touch_subwindow.h"
#include "include/gflib/strbuf.h"
#include "include/gflib/msg_print.h"

#include "include/system/fontproc.h"
#include "include/system/msgdata.h"
#include "include/system/arc_util.h"
#include "include/system/window.h"
#include "include/poketool/pokeicon.h"

#include "field/field_common.h"
#include "field/field_event.h"
#include "field/fieldsys.h"
#include "field/ev_mapchange.h"

#include "system/bmp_menu.h"
#include "system/palanm.h"
#include "system/clact_tool.h"
#include "system/wipe.h"
#include "system/pm_overlay.h"

#include "battle/battle_common.h"
#include "battle/graphic/batt_bg_gs_def.h"
#include "pokeanime/p_anm_sys.h"
#include "savedata/record.h"

#include "data/cb_data_def.h"
#include "cb_snd_def.h"
#include "cb_sys.h"


typedef struct {
	int wait;
	CATS_ACT_PTR	cap;
	FONTOAM_OBJ_PTR font_obj;
} TBUTTON_ANIME;

/////////////////////////////////////////////////////////////////////////////////////////////
///プロトタイプエリア

///シーケンス
static BOOL CB_Sub_Select(CB_MAIN_WORK* cbmw);
static BOOL CB_Sub_Edit(CB_MAIN_WORK* cbmw);
//static BOOL CB_Sub_SettingSet(CB_MAIN_WORK* cbmw);
static BOOL CB_Sub_SettingRemove(CB_MAIN_WORK* cbmw);
static BOOL CB_Sub_Change(CB_MAIN_WORK* cbmw);
static BOOL CB_Sub_End(CB_MAIN_WORK* cbmw);

static int editsub_Init(CB_MAIN_WORK* cbmw);
static int editsub_Exit(CB_MAIN_WORK* cbmw);
static int editsub_EditEnd(CB_MAIN_WORK* cbmw);
static int editsub_BallEffect(CB_MAIN_WORK* cbmw);

//入力
static int Input_KTStatusChange(CB_MAIN_WORK* cbmw);
static int Input_Select(CB_MAIN_WORK* cbmw);
static int input_SelectCommon(CB_MAIN_WORK* cbmw,u8 event);
static int keyin_Select(CB_MAIN_WORK* cbmw);
static int touchin_Select(CB_MAIN_WORK *cbmw);

static int Input_Change(CB_MAIN_WORK* cbmw);
static int input_ChangeCommon(CB_MAIN_WORK* cbmw,u8 event);
static int keyin_Change(CB_MAIN_WORK* cbmw);
static int touchin_Change(CB_MAIN_WORK *cbmw);

static int Input_Custom(CB_MAIN_WORK* cbmw);

static int touchin_Common(CB_MAIN_WORK *cbmw);
static BOOL	keyin_CursorMove(int* cur);
static void keyin_CursorMovePosUpdate(CB_MAIN_WORK* cbmw, int no, BOOL sub_auto);


//メニュー関連
static int CB_MENU_Cancel(CB_MAIN_WORK* cbmw);
static int CB_MENU_Edit(CB_MAIN_WORK* cbmw);
static int CB_MENU_Set(CB_MAIN_WORK* cbmw);
static int CB_MENU_Remove(CB_MAIN_WORK* cbmw);
static int CB_MENU_Change(CB_MAIN_WORK* cbmw);

///その他
static void BackupEditData_Make(CB_MAIN_WORK* cbmw);
static void BackupEditData_Recover(CB_MAIN_WORK* cbmw);
static void BackupItemData_Make(CB_MAIN_WORK* cbmw);
static void BackupItemData_Recover(CB_MAIN_WORK* cbmw);
static void CBT_TestPokemonCreate(CB_MAIN_WORK* cbmw);
static void CB_Sub_SeqChange(CB_MAIN_WORK* cbmw, int mode, int seq, int keep_seq);

static void btnobj_Anime(CATS_ACT_PTR cap, int event);
static void FontOamPosSet(FONTOAM_OBJ_PTR obj, int ox, int oy);

static void BA_Tcb(TCB_PTR tcb, void* work);
static void BA_Init(CATS_ACT_PTR cap, FONTOAM_OBJ_PTR font_obj);

static BOOL CB_Sys_IsSubTCB(CB_MAIN_WORK* cbmw);
static void CB_Sys_MainTPSet(CB_MAIN_WORK* cbmw, BOOL active);


static void CB_Test_PokemonSSP_Add(CB_MAIN_WORK* cbmw);
static void CB_Test_PokemonSSP_AnimeInit(CB_MAIN_WORK* cbmw);
static void CB_Test_PokemonSSP_AppearInit(CB_MAIN_WORK* cbmw);
static BOOL CB_Test_PokemonSSP_AppearMain(CB_MAIN_WORK* cbmw);
static BOOL CB_Test_PokemonSSP_ReturnMain(CB_MAIN_WORK* cbmw);
static void CB_Test_PokemonSSP_Vanish(CB_MAIN_WORK* cbmw, int flag);
static void CB_Test_PokemonSSP_Del(CB_MAIN_WORK* cbmw);





/////////////////////////////////////////////////////////////////////////////////////////////
//データテーブルエリア
static BOOL (* const CB_ModeTable[])(CB_MAIN_WORK* cbmw) = {
	CB_Sub_Select,
	CB_Sub_Edit,
//	CB_Sub_SettingSet,
	CB_Sub_SettingRemove,
	CB_Sub_Change,
	CB_Sub_End,
};

const TCB_MENU_PARAM CB_ParamTable[CB_DEF_MENU_MAX] = {
	{ msg_menu_00_01, (const u32)CB_MENU_Edit	},		///< へんしゅう
	{ msg_menu_00_02, (const u32)CB_MENU_Cancel },		///< やめる
	{ msg_menu_00_03, (const u32)CB_MENU_Set 	},		///< セット
	{ msg_menu_00_04, (const u32)CB_MENU_Remove },		///< はずす
	{ msg_menu_00_05, (const u32)CB_MENU_Change },		///< いれかえる
};

#define INFO_MSG_WAIT	(20)

/////////////////////////////////////////////////////////////////////////////////////////////
//ソースエリア

/*
 *	@brief	メインルーチン
 */
BOOL CustomMain_Executed(CB_MAIN_WORK* cbmw)
{
	BOOL active;

	active = CB_ModeTable[ cbmw->mode ]( cbmw );

	CATS_Draw(cbmw->sys.crp);

	return active;
}

/*
 *	@brief	ボール選択シーケンス　メイン
 */
static BOOL CB_Sub_Select(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->seq){
	case CBS_SELECT_INIT:
		{
			ARCHANDLE* p_handle;
			// カスタムボールグラフィックアーカイブハンドルオープン
			p_handle = ArchiveDataHandleOpen( ARC_CUSTOM_BALL_GRA, HEAPID_CUSTOM_BALL );

			///< OBJ+BG読み込み
			CB_Tool_PokeIcon_Load(cbmw);
			CB_Tool_PokeIcon_Add(cbmw);
			CB_Tool_PokeIcon_CheckPosSetup(cbmw);

			CB_Select_TopResLoad(cbmw,p_handle);
			CB_Select_BottomResLoad(cbmw,p_handle);
			CB_Edit_BottomResLoad(cbmw,p_handle);
			CB_Tool_MarkObj_SetUp(cbmw);
			CB_Tool_SubSelect_ObjAdd(cbmw,p_handle);
			CBDisp_ComWinAdd(cbmw);

			CBS_LoadInit(cbmw);
			CBS_Enable(cbmw, CATS_ENABLE_TRUE);
			CBS_Seal_ViewPosSet(cbmw);

			///< 各ボタンのリソース読み込み、登録、非表示
			CB_Tool_BTN_ResLoad(cbmw,p_handle);			///< ページ以外のOBJ
			CB_Tool_BTN_Add(cbmw);

			Piko_ResLoad(cbmw,p_handle);
			Piko_Add(cbmw);

			BackupEditData_Make(cbmw);
			BackupItemData_Make(cbmw);
			PageData_Create(cbmw, cbmw->page.no);	///< ページ構築
			PageData_ResourceLoad(cbmw);			///< リソース読み込み
			PageData_Add(cbmw);						///< OBJ登録

			CB_Tool_BTN_Enable(cbmw, CATS_ENABLE_FALSE);
			Piko_Enable(cbmw, CATS_ENABLE_FALSE);

			FontOam_Add(cbmw);
			FontOam_Enable(cbmw, FALSE);

			CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_SEL,FALSE);
			CB_DispOn();
			CB_Sys_MainTPSet(cbmw, FALSE);

			CBDisp_MainBtnSet(cbmw,TRUE);

			//上画面がサブ、下画面がメイン
			GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

			// カスタムボールグラフィックアーカイブハンドルクローズ
			ArchiveDataHandleClose( p_handle );
		}
		cbmw->seq++;
		break;

	case CBS_SELECT_WIPE_IN:
		///< ワイプ
		CB_WipeFadeIn();
		cbmw->seq++;
		break;

	case CBS_SELECT_WIPE_IN_WAIT:
		///< ワイプ待ち
		if (WIPE_SYS_EndCheck() != TRUE){ break; }
		cbmw->seq++;

	case CBS_SELECT_MAIN:
		cbmw->seq = Input_Select(cbmw);
		break;
	case CBS_SELECT_MENU:
		///< bmp
		{
			pCB_Menu_Func func;
			u32 ret = SelectBoxMain(cbmw->sys.list_wk);
			switch(ret){
			case SBOX_SELECT_NULL:
				break;
			case SBOX_SELECT_CANCEL:
				CB_MENU_Cancel(cbmw);
				CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_SEL,FALSE);
				cbmw->seq = CBS_SELECT_MAIN;
				break;
			default:
				func = (pCB_Menu_Func)ret;
				if (func != NULL){
					int func_ret = func(cbmw);
					if (func_ret != CB_ENUM_MENU_FUNC_RET_SEQ_PREV){
						CB_MENU_Cancel(cbmw);
						break;
					}

					CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_SEL,FALSE);
					cbmw->seq = CBS_SELECT_MAIN;
				}
				break;
			}
		}
		break;
	case CBS_SELECT_WIPE_OUT:
		///< 切り替え
		CB_WipeFadeOut();
		cbmw->seq++;
		break;

	case CBS_SELECT_WIPE_OUT_WAIT:
		///< ワイプまち
		if (WIPE_SYS_EndCheck() != TRUE){ break; }
		CBDisp_ComWinDel(cbmw);
		SubSelect_ObjDelete(cbmw);
		CB_Tool_PokeIcon_Del(cbmw);
		CB_Tool_BTN_Delete(cbmw);
		Piko_Delete(cbmw);
		PageData_Del(cbmw);					///< OBJ登録
		return FALSE;
	}
	CB_Tool_PokeIcon_Update(cbmw);
	SubSelect_ObjUpdate(cbmw);

	return TRUE;
}

#if 0
static BOOL CB_Sub_SettingSet(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->seq){
	case CBS_SET_INIT:
		cbmw->seq++;
	case CBS_SET_WIPE_OUT:
		CB_WipeFadeOut();
		cbmw->seq++;
		break;

	case CBS_SET_WIPE_OUT_WAIT:
		if (WIPE_SYS_EndCheck() != TRUE){ break; }
		GF_BGL_BmpWinDel(&cbmw->sys.win[ WIN_T_MAIN ]);
		SubSelect_ObjDelete(cbmw);
		CB_Tool_PokeIcon_Del(cbmw);
		CB_Tool_BTN_Delete(cbmw);
		Piko_Delete(cbmw);
		PageData_Del(cbmw);					///< OBJ登録
		CBProc_NextModeSet(cbmw->cb_pw, CB_PROC_NEXT_MODE_LIST);

		return FALSE;
	}

	CB_Tool_PokeIcon_Update(cbmw);
	SubSelect_ObjUpdate(cbmw);

	return TRUE;
}
#endif
static BOOL CB_Sub_Edit(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->seq){
	case CBS_CUSTOM_INIT:
		cbmw->seq = editsub_Init(cbmw);
		break;
	case CBS_CUSTOM_MAIN:
		///< メイン
		cbmw->seq = Input_Custom(cbmw);
		break;
	case CBS_CUSTOM_CON:
		///< パーティクル確認
		cbmw->seq = editsub_BallEffect(cbmw);
		break;
	case CBS_CUSTOM_DEC:
		///< 決定
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_EDIT_END,FALSE);
		cbmw->msg_wait = INFO_MSG_WAIT;	//ウェイト設定
		///< ソート
		if (CBS_CheckEditData(cbmw) == TRUE){
			cbmw->sys.bEdit = TRUE;
		}
		CBS_EditData_Save(cbmw);
		SubSelect_ObjRefrect(cbmw);

		{
			///< カウントアップ処理
			RECORD* rec;

			rec = SaveData_GetRecord(cbmw->cb_pw->save);

			RECORD_Score_Add(rec, SCORE_ID_CUSTOM_CAPSULE);
		}
		{

			POKEMON_PARAM* pp;

			///< 上書き
			if (cbmw->mo[ cbmw->select[ 0 ] ].poke_idx != CB_DEF_POKE_NOT_INDEX){

				pp = cbmw->cb_pw->poke_para[ cbmw->mo[ cbmw->select[ 0 ] ].poke_idx ];

				PokeParaPut(pp,
							ID_PARA_cb_core,
							CB_SaveData_CoreDataGet(cbmw->cb_pw->save_data, cbmw->select[ 0 ]));

			}
		}
		cbmw->seq = CBS_CUSTOM_EXIT;
		break;
	case CBS_CUSTOM_END:
		///< やめる
		cbmw->seq = editsub_EditEnd(cbmw);
		break;
	case CBS_CUSTOM_EXIT:
		if(!editsub_Exit(cbmw)){
			break;
		}
		return TRUE;
	};

	// 初期化時とメイン時しか動作しないタッチパネル操作
	if(cbmw->seq==CBS_CUSTOM_MAIN || cbmw->seq ==CBS_CUSTOM_INIT ){
		///< タッチパネル
		if (cbmw->sys.hold == CB_DEF_NOT_HOLD ){
			BMN_Main(cbmw->sys.btn);
		}
		else {
			u32 x, y;
			int id;
			BOOL bHit;

			id = cbmw->sys.hold;

			bHit = GF_TP_GetPointCont(&x, &y);

			if(cbmw->seq != CBS_CUSTOM_MAIN){
				bHit = 0;
			}

			if (bHit){
				int name_id;
				name_id = Seal_NameGet(cbmw->seal[id].id);
				CB_BMP_SealNameSet(&cbmw->sys.win[WIN_T_MAIN], name_id);
				CATS_ObjectPosSetCap(cbmw->seal[id].cap, x, y);
				CBS_ReleasePointCheckTypeSet(cbmw, id);
				CBS_EditDataRefrect(cbmw, id);
			}
			else {
				BOOL release;

				release = CBS_ReleasePointCheck(cbmw, id);
				HitTable_CreateCap(cbmw->seal[id].hit, cbmw->seal[id].cap, FALSE);

				if (release == FALSE){
					CB_SaveData_ItemNumAddCheck_CBO(cbmw->cb_pw->save_data, cbmw->seal[id].id, +1);
					CB_BMP_SealNumWinPrint(cbmw);
					CBS_Release(cbmw, id);
				}

				Snd_SePlay( CB_SE_DROP );
				cbmw->sys.hold = CB_DEF_NOT_HOLD;
				CB_BMP_SealNameSet(&cbmw->sys.win[WIN_T_MAIN], CB_MSG_ID_CLEAR);
			}
		}
	}

	CB_Tool_BTN_Update(cbmw);
	Piko_Update(cbmw);
	CB_Tool_PokeIcon_Update(cbmw);
	SubSelect_ObjUpdate(cbmw);

	return TRUE;
}


static BOOL CB_Sub_SettingRemove(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->seq){
	case CBS_REM_INIT:
		CB_Tool_CoreDataRemove(cbmw, cbmw->select[0]);
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_CB_REM,TRUE);
		cbmw->seq++;
		break;

	case CBS_REM_MAIN:
		Input_KTStatusChange(cbmw);
		if (sys.trg & CB_DEF_MSG_END_KEY || GF_TP_GetTrg()){
			cbmw->seq++;
		}
		break;

	case CBS_REM_END:
		CB_Sub_SeqChange(cbmw, CB_MODE_SELECT, CB_DEF_KEEP_SEQ_SET, 0);
		//BmpTalkWinClear(&cbmw->sys.win[ WIN_T_MAIN ], WINDOW_TRANS_OFF);
		BmpMenuWinClear(&cbmw->sys.win[ WIN_T_MAIN ], WINDOW_TRANS_OFF);
		GF_BGL_BmpWinOffVReq(&cbmw->sys.win[ WIN_T_MAIN ]);
		CBDisp_MainBtnSet(cbmw,TRUE);
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_SEL,FALSE);
		break;
	}

	CB_Tool_PokeIcon_Update(cbmw);
	SubSelect_ObjUpdate(cbmw);

	return TRUE;
}

static BOOL CB_Sub_Change(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->seq){
	case CBS_CHANGE_INIT:
		CATS_ObjectPriSetCap(cbmw->cap_cur[0], CB_ENUM_OAM_PRI_CUR_2);
		CATS_ObjectPriSetCap(cbmw->cap_cur[1], CB_ENUM_OAM_PRI_CUR_1);
		CATS_ObjectAnimeSeqSetCap(cbmw->cap_cur[0], 1);
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_REP,FALSE);
		CATS_ObjectEnableCap(cbmw->cap_cur[1], CATS_ENABLE_TRUE);
		cbmw->seq++;

	case CBS_CHANGE_MAIN:
		cbmw->seq = Input_Change(cbmw);
		break;
	case CBS_CHANGE_KEY_WAIT:
		Input_KTStatusChange(cbmw);
		if (sys.trg & CB_DEF_MSG_END_KEY || GF_TP_GetTrg()){
			cbmw->seq = CBS_CHANGE_END;
		}
		break;

	case CBS_CHANGE_END:
		CATS_ObjectPriSetCap(cbmw->cap_cur[0], CB_ENUM_OAM_PRI_CUR_1);
		CATS_ObjectPriSetCap(cbmw->cap_cur[1], CB_ENUM_OAM_PRI_CUR_2);
		CB_Sub_SeqChange(cbmw, CB_MODE_SELECT, CB_DEF_KEEP_SEQ_SET, 0);
		CATS_ObjectAnimeSeqSetCap(cbmw->cap_cur[0], 0);
//		BmpTalkWinClear(&cbmw->sys.win[ WIN_T_MAIN ], WINDOW_TRANS_OFF);
		BmpMenuWinClear(&cbmw->sys.win[ WIN_T_MAIN ], WINDOW_TRANS_OFF);
		GF_BGL_BmpWinOffVReq(&cbmw->sys.win[ WIN_T_MAIN ]);
		CBDisp_MainBtnSet(cbmw,TRUE);
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_SEL,FALSE);
		break;
	}

	CB_Tool_PokeIcon_Update(cbmw);
	SubSelect_ObjUpdate(cbmw);

	return TRUE;
}

static BOOL CB_Sub_End(CB_MAIN_WORK* cbmw)
{
	return FALSE;
}

/**
 *	@brief	セレクト画面からエディット画面への移行処理
 */
static int editsub_Init(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->sub_seq){
	case 0:
		///< 初期化
		CB_Sys_MainTPSet(cbmw, FALSE);

		CB_BMP_SealNumWinAdd(cbmw);
		CB_BMP_SealNumWinPrint(cbmw);

		BackupEditData_Make(cbmw);
		BackupItemData_Make(cbmw);

		cbmw->sys.bEdit = FALSE;
		cbmw->msg_wait = 0;
		//フェードアウトリクエスト
		CB_WipeFadeOut();
		cbmw->sub_seq++;
		break;
	case 1:
		///< 上画面一部＋OBJフェード
		if (CB_Sys_IsSubTCB(cbmw) == TRUE){
			break;
		}
		if (WIPE_SYS_EndCheck() != TRUE){
			break;
		}
		///< 優先度変更
		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_OFF);
		GF_Disp_GXS_VisibleControl(CB_PLANE_LIST, VISIBLE_ON);
		GF_BGL_PrioritySet(CB_DEF_EDIT_SUB_BG_FRM, CB_BG_PRI_SCR_EDIT_TOP);
		CB_Tool_BTN_Enable(cbmw, CATS_ENABLE_TRUE);
		Piko_Enable(cbmw, CATS_ENABLE_TRUE);
		FontOam_Enable(cbmw, TRUE);
		CB_View_ScrollSet(cbmw, 1);

		//金銀BTS4830対処
		CATS_ObjectAnimeFrameSetCap(cbmw->button_obj.cap[CB_ENUM_BHIT_UP], 0);
		CATS_ObjectAnimeFrameSetCap(cbmw->button_obj.cap[CB_ENUM_BHIT_DOWN], 0);
		CATS_ObjectAnimeFrameSetCap(cbmw->button_obj.cap[CB_ENUM_BHIT_CON], 0);

		GF_BGL_PrioritySet(CB_DEF_FIELD_BG_FRM, CB_BG_PRI_SCR_FIELD_TOP);

		//メッセージ描画
//		BmpTalkWinClear(&cbmw->sys.win[ WIN_B_MAIN ], WINDOW_TRANS_ON);
		GF_BGL_BmpWinOff(&cbmw->sys.win[ WIN_B_MAIN] );
#if 0
		CB_BMP_WindowAdd(cbmw->sys.bgl, &cbmw->sys.win[WIN_T_MAIN], CB_DEF_STRING_BG_FRM,
						 BMPL_T_MAIN_X, BMPL_T_MAIN_Y,
						 BMPL_T_MAIN_SX, BMPL_T_MAIN_SY, BMPL_T_MAIN_CGX);
#endif
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_CLEAR,TRUE);

		//上画面がメイン、下画面がサブ
		GX_SetDispSelect(GX_DISP_SELECT_MAIN_SUB);
		CB_WipeFadeIn();
		cbmw->sub_seq++;
		break;
	case 2:
		if (WIPE_SYS_EndCheck() != TRUE){
			break;
		}
		CB_Sys_MainTPSet(cbmw, TRUE);
		cbmw->sub_seq = 0;
		return CBS_CUSTOM_MAIN;
	}
	return CBS_CUSTOM_INIT;
}

/**
 *	@brief	エディット画面からセレクト画面へ戻る処理
 **/
static int editsub_Exit(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->sub_seq){
	case 0:
		if(cbmw->msg_wait > 0){
			--cbmw->msg_wait;
			return FALSE;
		}
		cbmw->msg_wait = 0;
		CB_WipeFadeOut();
		cbmw->sub_seq++;
		break;
	case 1:
		if (WIPE_SYS_EndCheck() != TRUE){
			break;
		}
		///< バトル背景フェード
		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_ON);

		GF_BGL_PrioritySet(CB_DEF_FIELD_BG_FRM, CB_BG_PRI_SCR_FIELD);
		GF_BGL_PrioritySet(CB_DEF_EDIT_SUB_BG_FRM, CB_BG_PRI_SCR_EDIT);
		GF_Disp_GXS_VisibleControl(CB_PLANE_LIST, VISIBLE_OFF);
		CB_Tool_BTN_Enable(cbmw, CATS_ENABLE_FALSE);
		Piko_Enable(cbmw, CATS_ENABLE_FALSE);
		FontOam_Enable(cbmw, FALSE);

//		BmpTalkWinClear(&cbmw->sys.win[ WIN_T_MAIN ], WINDOW_TRANS_ON);
		BmpMenuWinClear(&cbmw->sys.win[ WIN_T_MAIN ], WINDOW_TRANS_OFF);

		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_SEL,FALSE);
		GF_BGL_BmpWinOn(&cbmw->sys.win[ WIN_B_MAIN ]);

		CBDisp_MainBtnSet(cbmw,TRUE);

		//上画面がサブ、下画面がメイン
		GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

		CB_View_ScrollSet(cbmw, -1);
		CB_Sys_MainTPSet(cbmw, FALSE);

		CB_WipeFadeIn();
		cbmw->sub_seq++;
		break;
	case 2:
		///< フェード待ち
		if (CB_Sys_IsSubTCB(cbmw) == TRUE){
			break;
		}
		if (WIPE_SYS_EndCheck() != TRUE){
			break;
		}
		CATS_ObjectEnableCap(cbmw->cap_cur[1], CATS_ENABLE_FALSE);
		CB_Sub_SeqChange(cbmw, CB_MODE_SELECT, CB_DEF_KEEP_SEQ_SET, 0);
		CB_BMP_SealNumWinDel(cbmw);

		cbmw->sub_seq = 0;
		return TRUE;
	};
	return FALSE;
}

/**
 *	@brief	エディット画面からセレクト画面へリターン
 */
static int editsub_EditEnd(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->sub_seq){
	case 0:
		///< 薄暗く
		if (CBS_CheckEditData(cbmw) == FALSE){
			return CBS_CUSTOM_EXIT;
		}
		PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_BG,
					   (1 << CB_ENUM_PAL_BALL_BG) |
					   (1 << CB_ENUM_PAL_EDIT_BG) |
					   (1 << CB_ENUM_PAL_LIST_BG) |
					   (1 << CB_ENUM_PAL_NUM), 0, 0, CB_DEF_FADE_GRAY, 0);
		PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_OBJ, 0xFFFF, 0,  0, CB_DEF_FADE_GRAY, 0);
		CB_Sys_MainTPSet(cbmw, FALSE);
		GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG0, VISIBLE_OFF);
		cbmw->sub_seq++;
		break;

	case 1:
		///< チェック
		if (PaletteFadeCheck(cbmw->sys.pfd) != 0){ break; }
		PaletteTrans_AutoSet(cbmw->sys.pfd, FALSE);
		CB_BMP_WindowAdd(cbmw->sys.bgl, &cbmw->sys.win[ WIN_B_MSG ], CB_DEF_STRING_SUB_BG_FRM,
						 BMPL_B_MSG_X,  BMPL_B_MSG_Y,
						 BMPL_B_MSG_SX, BMPL_B_MSG_SY, BMPL_B_MSG_CGX);
		{
			TOUCH_SW_PARAM tsp;

			MI_CpuClear8(&tsp,sizeof(TOUCH_SW_PARAM));
			tsp.p_bgl	  = cbmw->sys.bgl;
			tsp.bg_frame  = CB_DEF_STRING_SUB_BG_FRM;
			tsp.char_offs = CB_DEF_TPBG_CHAR_OFS;
			tsp.pltt_offs = CB_ENUM_PAL_SWITCH;
			tsp.x		  = CB_DEF_SW_X;
			tsp.y		  = CB_DEF_SW_Y;
			tsp.kt_st	  = cbmw->key_mode;

			cbmw->sys.tss = TOUCH_SW_AllocWork(HEAPID_CUSTOM_BALL);

			TOUCH_SW_Init(cbmw->sys.tss, &tsp);
		}
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MSG], WIN_B_MSG, CB_MSG_ID_END_QUE,FALSE);
		cbmw->sub_seq++;
		break;

	case 2:
		GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG0, VISIBLE_ON);
		cbmw->sub_seq++;

	case 3:
		{
			u32 ret;

			ret = TOUCH_SW_Main(cbmw->sys.tss);

			switch(ret){
			case TOUCH_SW_RET_YES:
			case TOUCH_SW_RET_NO:
				cbmw->key_mode = TOUCH_SW_GetKTStatus(cbmw->sys.tss);
				cbmw->sub_work = ret;
				PaletteTrans_AutoSet(cbmw->sys.pfd, TRUE);
				TOUCH_SW_Reset(cbmw->sys.tss);
				TOUCH_SW_FreeWork(cbmw->sys.tss);
				BmpTalkWinClear(&cbmw->sys.win[ WIN_B_MSG ], WINDOW_TRANS_ON);
				GF_BGL_BmpWinDel(&cbmw->sys.win[ WIN_B_MSG ]);
				cbmw->sub_seq++;
				break;

			case TOUCH_SW_RET_NORMAL:
			default:
				break;
			}
		}
		break;

	case 4:
		PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_BG,
					   (1 << CB_ENUM_PAL_BALL_BG) |
					   (1 << CB_ENUM_PAL_EDIT_BG) |
					   (1 << CB_ENUM_PAL_LIST_BG) |
					   (1 << CB_ENUM_PAL_NUM), 0,  CB_DEF_FADE_GRAY, 0, 0);
		PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_OBJ, 0xFFFF, 0,  CB_DEF_FADE_GRAY, 0, 0);
		cbmw->sub_seq++;
		break;

	case 5:
		{
		int ret;

			if (PaletteFadeCheck(cbmw->sys.pfd) != 0){ break; }
			switch(cbmw->sub_work){
			case TOUCH_SW_RET_YES:
				BackupEditData_Recover(cbmw);
				BackupItemData_Recover(cbmw);
				CB_BMP_SealNumWinPrint(cbmw);
				CBS_Delete(cbmw);
				CBS_LoadInit(cbmw);
				CBS_Enable(cbmw, CATS_ENABLE_TRUE);
				ret = CBS_CUSTOM_DEC;
				cbmw->msg_wait = INFO_MSG_WAIT;	//ウェイト設定
				break;
			case TOUCH_SW_RET_NO:
				ret = CBS_CUSTOM_MAIN;
				break;
			case TOUCH_SW_RET_NORMAL:
			default:
				ret = CBS_CUSTOM_END;
				break;
			}
			CB_Sys_MainTPSet(cbmw, TRUE);
			cbmw->sub_seq = 0;
			return ret;
		}
	}
	return CBS_CUSTOM_END;
}

/**
 *	@brief	ボールエフェクト再生
 */
static int editsub_BallEffect(CB_MAIN_WORK* cbmw)
{
	switch(cbmw->sub_seq){
	case 0:
		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_ON);
		PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_BG,
					   (1 << CB_ENUM_PAL_BALL_BG) |
					   (1 << CB_ENUM_PAL_EDIT_BG) |
					   (1 << CB_ENUM_PAL_LIST_BG) |
					   (1 << CB_ENUM_PAL_NUM), 0, 0, CB_DEF_FADE_GRAY, 0);
		PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_OBJ, 0xFFFF, 0,  0, CB_DEF_FADE_GRAY, 0);
		CB_Sys_MainTPSet(cbmw, FALSE);
		cbmw->sub_work = 0;
		CBT_TestPokemonCreate(cbmw);
		CB_Test_PokemonSSP_Add(cbmw);
		CB_Test_PokemonSSP_Vanish(cbmw, 1);
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_CON,FALSE);
		cbmw->sub_seq++;
		break;

	case 1:
		///< ボールなげる
		if (PaletteFadeCheck(cbmw->sys.pfd) != 0){ break; }

		{
			TBALL_MOVE_DATA bmd;

			bmd.type	= EBMT_STAY_BB;
			bmd.heap_id	= HEAPID_CUSTOM_BALL;
			bmd.mode	= EBMM_THROW;
			bmd.id		= BM_SUITABLE_ID;
			bmd.bg_pri	= BM_BG_PRI_CUSTOM;
			bmd.surface = 1;
			bmd.csp		= cbmw->sys.csp;
			bmd.pfd		= cbmw->sys.pfd;
			bmd.ball_id = PokeParaGet(cbmw->test_pp, ID_PARA_get_ball, NULL);

			cbmw->sys.bms	= BM_Init(&bmd);
			cbmw->sub_seq++;
		}
		break;
	case 2:
		///<
		{
			int		test_id;
			CB_CORE	test_core;
			TBALL_CUSTOM_PARAM bcp = { 0 };

			bcp.client_type = CLIENT_TYPE_BB;
			bcp.pp			= cbmw->test_pp;

			test_id = cbmw->select[0] + 1;
			CBS_TestCoreData_Create(&test_core, cbmw);

			PokeParaPut(cbmw->test_pp, ID_PARA_cb_id,	(u8*)&test_id);
			PokeParaPut(cbmw->test_pp, ID_PARA_cb_core,	&test_core);

			cbmw->sys.bes	= BallEffect_Init(HEAPID_CUSTOM_BALL, &bcp);

			BallEffect_ResourceLoad(cbmw->sys.bes);
		}
		cbmw->sub_seq++;
		cbmw->sub_work = 0;
		break;
	case 3:
		if (BallEffect_IsLoadEnd(cbmw->sys.bes) != TRUE){
			break;
		}

		if (BM_IsOpen(cbmw->sys.bms) == TRUE){
			CB_Test_PokemonSSP_AppearMain(cbmw);
			BallEffect_ExecutedEz(cbmw->sys.bes);

			CB_Test_PokemonSSP_Vanish(cbmw, 0);
			CB_Test_PokemonSSP_AppearInit(cbmw);

			Snd_SePlay( CB_SE_BOMB );

			cbmw->sub_seq++;
		}
		break;
	case 4:
		{
			BOOL ret = CB_Test_PokemonSSP_AppearMain(cbmw);

			if (BM_EndCheck(cbmw->sys.bms) != FALSE){ break; }

			if (ret == FALSE){
				BM_Delete(cbmw->sys.bms);
				GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_OFF);

				CB_Test_PokemonSSP_AnimeInit(cbmw);
				cbmw->sub_work = 0;
				cbmw->sub_seq++;
			}
		}
		break;
	case 5:
		if (BallEffect_EndCheck(cbmw->sys.bes) != FALSE){ break; }
		if (PokeAnm_IsFinished(cbmw->sys.pas, 0) != TRUE){ break; }
		if (SoftSpriteAnimeEndCheck(cbmw->sys.ssp) != FALSE){ break; }
		BallEffect_FreeMemory(cbmw->sys.bes);
		cbmw->sub_seq++;
		break;
	case 6:
		if ((++cbmw->sub_work) < CB_DEF_POKE_RETURN_WAIT){ break; }
		cbmw->sub_work = 0;
		{
			TBALL_MOVE_DATA bmd;

			bmd.type	= EBMT_STAY_BB;
			bmd.heap_id	= HEAPID_CUSTOM_BALL;
			bmd.mode	= EBMM_CLOSE;
			bmd.id		= BM_SUITABLE_ID;
			bmd.bg_pri	= BM_BG_PRI_CUSTOM;
			bmd.surface = 1;
			bmd.csp		= cbmw->sys.csp;
			bmd.pfd		= cbmw->sys.pfd;
			bmd.ball_id = PokeParaGet(cbmw->test_pp, ID_PARA_get_ball, NULL);

			cbmw->sys.bms	= BM_Init(&bmd);

			CB_Tool_DefaultBlendSet();
			GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_ON);

			cbmw->sub_seq++;
		}
		break;
	case 7:
		{
			BOOL ret = CB_Test_PokemonSSP_ReturnMain(cbmw);

			if (BM_EndCheck(cbmw->sys.bms) == FALSE && ret == FALSE){
				PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_BG,
							   (1 << CB_ENUM_PAL_BALL_BG) |
							   (1 << CB_ENUM_PAL_EDIT_BG) |
							   (1 << CB_ENUM_PAL_LIST_BG) |
							   (1 << CB_ENUM_PAL_NUM), 0,  CB_DEF_FADE_GRAY, 0, 0);
				PaletteFadeReq(cbmw->sys.pfd, PF_BIT_SUB_OBJ, 0xFFFF, 0,  CB_DEF_FADE_GRAY, 0, 0);
				CB_Test_PokemonSSP_Vanish(cbmw, 1);
				CB_Test_PokemonSSP_Del(cbmw);
				BM_Delete(cbmw->sys.bms);
				cbmw->sub_seq++;
			}
		}
		break;
	default:
		if (PaletteFadeCheck(cbmw->sys.pfd) != 0){ break; }
		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_OFF);
		CB_Sys_MainTPSet(cbmw, TRUE);
		cbmw->sub_seq = 0;
		cbmw->seq = CBS_CUSTOM_MAIN;
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_CLEAR,FALSE);
		return CBS_CUSTOM_MAIN;
	}
	return CBS_CUSTOM_CON;
}

void CB_ButtonCallBack( u32 button, u32 event, void* work )
{
	CB_MAIN_WORK* cbmw = (CB_MAIN_WORK*)work;

	if (cbmw->sys.main_tp_active == FALSE){	return;	}

	switch(button){
	case CB_ENUM_BHIT_UP:
		///< ページ↑切り替え
		if (event == BMN_EVENT_TOUCH){
			if (cbmw->page.no > 0){
				cbmw->page.no--;
			}
			else {
				cbmw->page.no = cbmw->page.max - 1;
			}

			PageData_Del(cbmw);
			PageData_Create(cbmw, cbmw->page.no);
			PageData_ResourceLoad(cbmw);
			PageData_Add(cbmw);
			CB_BMP_SealNumWinPrint(cbmw);

			Snd_SePlay( CB_SE_UP );
		}
		btnobj_Anime(cbmw->button_obj.cap[CB_ENUM_BHIT_UP], event);
		break;

	case CB_ENUM_BHIT_DOWN:
		///< ページ↓切り替え
		if (event == BMN_EVENT_TOUCH){
			cbmw->page.no++;
			cbmw->page.no %= cbmw->page.max;

			PageData_Del(cbmw);
			PageData_Create(cbmw, cbmw->page.no);
			PageData_ResourceLoad(cbmw);
			PageData_Add(cbmw);
			CB_BMP_SealNumWinPrint(cbmw);

			Snd_SePlay( CB_SE_DOWN );
		}
		btnobj_Anime(cbmw->button_obj.cap[CB_ENUM_BHIT_DOWN], event);
		break;

	case CB_ENUM_BHIT_CON:
		///< 確認ボタン
		if (event == BMN_EVENT_TOUCH){
			if (cbmw->seq != CBS_CUSTOM_CON){
				cbmw->seq = CBS_CUSTOM_CON;

				Snd_SePlay( CB_SE_PLAY );
			}
			BA_Init(cbmw->button_obj.cap[CB_ENUM_BHIT_CON], NULL);
		}
		btnobj_Anime(cbmw->button_obj.cap[CB_ENUM_BHIT_CON], event);
		break;

	case CB_ENUM_BHIT_DEC:
		///< 決定ボタン
		if (event == BMN_EVENT_TOUCH){
			if (cbmw->seq != CBS_CUSTOM_DEC){
				cbmw->seq = CBS_CUSTOM_DEC;

				CB_Sys_MainTPSet(cbmw, FALSE);
				Snd_SePlay( CB_SE_DEC );
			}
			BA_Init(cbmw->button_obj.cap[CB_ENUM_BHIT_DEC], cbmw->sys.font_obj[0]);
		}
		btnobj_Anime(cbmw->button_obj.cap[CB_ENUM_BHIT_DEC], event);
		break;

	case CB_ENUM_BHIT_END:
		///< やめるボタン
		if (event == BMN_EVENT_TOUCH){
			if (cbmw->seq != CBS_CUSTOM_END){
				cbmw->seq = CBS_CUSTOM_END;

				CB_Sys_MainTPSet(cbmw, FALSE);
				Snd_SePlay( CB_SE_CAN );
			}
			BA_Init(cbmw->button_obj.cap[CB_ENUM_BHIT_END], cbmw->sys.font_obj[1]);
		}
		btnobj_Anime(cbmw->button_obj.cap[CB_ENUM_BHIT_END], event);
		break;

	case CB_ENUM_BHIT_SEAL_1:
	case CB_ENUM_BHIT_SEAL_2:
	case CB_ENUM_BHIT_SEAL_3:
	case CB_ENUM_BHIT_SEAL_4:
	case CB_ENUM_BHIT_SEAL_5:
	case CB_ENUM_BHIT_SEAL_6:
	case CB_ENUM_BHIT_SEAL_7:
	case CB_ENUM_BHIT_SEAL_8:
		///< シールポップボタン
		{
			int name_id;
			if (event == BMN_EVENT_TOUCH){

				if (CBS_IsPop(cbmw) == FALSE){
					///< もう空きが無い

					Snd_SePlay( CB_SE_LIMIT );

					CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_NG,FALSE);
				}
				else {
					if (cbmw->page.item[button] != 0
					&&	CB_SaveData_ItemNumGet_CBO(cbmw->cb_item_data, cbmw->page.item[button] - 1) != 0){

						cbmw->sys.hold = CBS_SearchPop(cbmw, button);

						name_id = Seal_NameGet(cbmw->page.item[button]);

						CB_BMP_SealNameSet(&cbmw->sys.win[WIN_T_MAIN], name_id);

						CB_SaveData_ItemNumAddCheck(cbmw->cb_pw->save_data, cbmw->page.item[button], -1);
						CB_BMP_SealNumWinPrint_No(cbmw, button);

						Snd_SePlay( CB_SE_POP );
					}
					else {
						if (cbmw->page.item[button] != 0){
							Snd_SePlay( CB_SE_LIMIT );
							CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_ZERO,FALSE);
						}
					}
				}
			}
		}
		break;

	case CB_BHIT_SEAL_0:
	case CB_BHIT_SEAL_1:
	case CB_BHIT_SEAL_2:
	case CB_BHIT_SEAL_3:
	case CB_BHIT_SEAL_4:
	case CB_BHIT_SEAL_5:
	case CB_BHIT_SEAL_6:
	case CB_BHIT_SEAL_7:
		///< ドロップシール
		{
			int id;
			int name_id;

			if (event == BMN_EVENT_TOUCH){

				id = button - CB_BHIT_SEAL_0;

				CBS_SealHold(cbmw, event, id);
				OS_Printf("id 1= %2d\n", id);

				name_id = Seal_NameGet(cbmw->seal[id].id);
				OS_Printf("id 2= %2d\n", cbmw->seal[id].id);

				CB_BMP_SealNameSet(&cbmw->sys.win[WIN_T_MAIN], name_id);

				Snd_SePlay( CB_SE_POP );
			}
		}
		break;

	default:
		GF_ASSERT(0);
		break;
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////
//入力

/**
 *	@brief	キーorタッチ切替コモン処理
 */
static int Input_KTStatusChange(CB_MAIN_WORK* cbmw)
{
	if(cbmw->key_mode == APP_KTST_TOUCH){
		if(GF_TP_GetCont() != 0){	//
			return 0;
		}
		if(sys.cont != 0){
			cbmw->key_mode = APP_KTST_KEY;
			return 1;
		}
	}else{
		if(sys.cont != 0){
			return 0;
		}
		if(GF_TP_GetCont() != 0){
			cbmw->key_mode = APP_KTST_TOUCH;
			return 0;
		}
	}
	return 0;
}

/**
 *	@brief	選択画面入力処理
 */
static int Input_Select(CB_MAIN_WORK* cbmw)
{
	Input_KTStatusChange(cbmw);
	if(cbmw->key_mode == APP_KTST_KEY){
		return keyin_Select(cbmw);
	}else{
		return touchin_Select(cbmw);
	}
	return CBS_SELECT_MAIN;
}

/**
 *	@brief	選択画面　入力共通処理
 */
static int input_SelectCommon(CB_MAIN_WORK* cbmw,u8 event)
{
	switch(event){
	case 12:	//Cancel
		Snd_SePlay( SEQ_SE_DP_DECIDE );
		CBProc_NextModeSet(cbmw->cb_pw, CB_PROC_NEXT_MODE_END);
		return CBS_SELECT_WIPE_OUT;
	case 13:	//Decide
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_HDYD,FALSE);
		CB_BMP_SelectWinAdd(cbmw->sys.bgl, &cbmw->sys.win[1], CB_DEF_STRING_BG_FRM, cbmw, cbmw->select[0]);
		Snd_SePlay( CB_SE_DEC_S );
		return CBS_SELECT_MENU;
	default:
		CBS_SVSort(cbmw);
		CBS_EditData_Save(cbmw);
		SubSelect_ObjRefrect(cbmw);
		cbmw->select[0] = event;
		keyin_CursorMovePosUpdate(cbmw, 0, TRUE);
		CBS_Delete(cbmw);
		CBS_LoadInit(cbmw);
		CBS_Enable(cbmw, CATS_ENABLE_TRUE);
		CBS_Seal_ViewPosSet(cbmw);

		if(cbmw->key_mode == APP_KTST_KEY){
			Snd_SePlay( CB_SE_CURSOR );
		}else{
			CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_B_MAIN], WIN_B_MAIN, CB_MSG_ID_CB_HDYD,FALSE);
			CB_BMP_SelectWinAdd(cbmw->sys.bgl, &cbmw->sys.win[1], CB_DEF_STRING_BG_FRM, cbmw, cbmw->select[0]);
			Snd_SePlay( CB_SE_DEC_S );
			return CBS_SELECT_MENU;
		}
		break;
	}
	return CBS_SELECT_MAIN;
}

/**
 *	@brief	選択画面　キー入力
 */
static int keyin_Select(CB_MAIN_WORK* cbmw)
{
	BOOL is_input;
	int  now;

	if(sys.trg & PAD_BUTTON_CANCEL){
		return input_SelectCommon(cbmw,12);
	}else if(sys.trg & PAD_BUTTON_DECIDE){
		return input_SelectCommon(cbmw,13);
	}
	if(!(sys.repeat & (PAD_KEY_LEFT|PAD_KEY_RIGHT|PAD_KEY_UP|PAD_KEY_DOWN))){
		return CBS_SELECT_MAIN;
	}

	now = cbmw->select[0];
	is_input = keyin_CursorMove(&now);
//	is_input = keyin_CursorMove(&(cbmw->select[0]));
//	now = cbmw->select[0];
//	cbmw->select[0] = old;

	if (is_input == FALSE){
		return CBS_SELECT_MAIN;
	}
	return input_SelectCommon(cbmw,now);
}


/**
 *	@brief	選択画面タッチ入力処理
 */
static int touchin_Select(CB_MAIN_WORK *cbmw)
{
	int ret;

	ret = touchin_Common(cbmw);

	if(ret < 0){
		return CBS_SELECT_MAIN;
	}
	return input_SelectCommon(cbmw,ret);
}

/**
 *	@brief	入れ替え画面入力処理
 */
static int Input_Change(CB_MAIN_WORK* cbmw)
{
	Input_KTStatusChange(cbmw);
	if(cbmw->key_mode == APP_KTST_KEY){
		return keyin_Change(cbmw);
	}else{
		return touchin_Change(cbmw);
	}
	return CBS_CHANGE_MAIN;
}

/**
 *	@brief	入れ替え画面　キャンセル処理
 */
static int inputsub_ChangeCommonDC(CB_MAIN_WORK* cbmw,BOOL decide)
{
	if(cbmw->select[0] == cbmw->select[1]){
		decide = FALSE;
	}
	if(decide){	//決定
		CB_Tool_CoreDataChange(cbmw, cbmw->select[0], cbmw->select[1]);
		keyin_CursorMovePosUpdate(cbmw, 1, TRUE);
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], WIN_T_MAIN, CB_MSG_ID_CB_REP_END,TRUE);
		Snd_SePlay( CB_SE_DEC_S );
		return CBS_CHANGE_KEY_WAIT;
	}else{	//キャンセル
		CATS_ObjectEnableCap(cbmw->cap_cur[1], CATS_ENABLE_FALSE);
		CATS_ObjectAnimeSeqSetCap(cbmw->cap_cur[0], 0);
		keyin_CursorMovePosUpdate(cbmw, 0, FALSE);

		Snd_SePlay( SEQ_SE_DP_DECIDE );
		return CBS_CHANGE_END;
	}
}

/**
 *	@brief	入れ替え画面　入力共通処理
 */
static int input_ChangeCommon(CB_MAIN_WORK* cbmw,u8 event)
{
	switch(event){
	case 12:	//Cancel
		return inputsub_ChangeCommonDC(cbmw,FALSE);
#if 0
		CATS_ObjectEnableCap(cbmw->cap_cur[1], CATS_ENABLE_FALSE);
		CATS_ObjectAnimeSeqSetCap(cbmw->cap_cur[0], 0);
		keyin_CursorMovePosUpdate(cbmw, 0, FALSE);

		Snd_SePlay( SEQ_SE_DP_DECIDE );
		return CBS_CHANGE_END;
#endif
	case 13:	//Decide
#if 0
		CB_Tool_CoreDataChange(cbmw, cbmw->select[0], cbmw->select[1]);
		keyin_CursorMovePosUpdate(cbmw, 1, TRUE);
		CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], CB_MSG_ID_CB_REP_END,TRUE);
		Snd_SePlay( CB_SE_DEC_S );
		return CBS_CHANGE_KEY_WAIT;
#endif
		return inputsub_ChangeCommonDC(cbmw,TRUE);
	default:
		cbmw->select[1] = event;
		keyin_CursorMovePosUpdate(cbmw, 1, FALSE);

		if(cbmw->key_mode == APP_KTST_KEY){
			Snd_SePlay( CB_SE_CURSOR );
		}else{
#if 0
			CB_Tool_CoreDataChange(cbmw, cbmw->select[0], cbmw->select[1]);
			keyin_CursorMovePosUpdate(cbmw, 1, TRUE);
			CB_BMP_InfoMessageSet(&cbmw->sys.win[WIN_T_MAIN], CB_MSG_ID_CB_REP_END,TRUE);
			Snd_SePlay( CB_SE_DEC_S );
			return CBS_CHANGE_KEY_WAIT;
#endif
			return inputsub_ChangeCommonDC(cbmw,TRUE);
		}
		break;
	}
	return CBS_CHANGE_MAIN;
}

/**
 *	@brief	入れ替え画面　キー入力
 */
static int keyin_Change(CB_MAIN_WORK* cbmw)
{
	BOOL is_input;
	int  now;

	if(sys.trg & PAD_BUTTON_CANCEL){
		return input_ChangeCommon(cbmw,12);
	}else if(sys.trg & PAD_BUTTON_DECIDE){
		return input_ChangeCommon(cbmw,13);
	}
	if(!(sys.repeat & (PAD_KEY_LEFT|PAD_KEY_RIGHT|PAD_KEY_UP|PAD_KEY_DOWN))){
		return CBS_CHANGE_MAIN;
	}

	now = cbmw->select[1];
	is_input = keyin_CursorMove(&now);

	if (is_input == FALSE){
		return CBS_CHANGE_MAIN;
	}
	return input_ChangeCommon(cbmw,now);
}


/**
 *	@brief	入れ替え画面タッチ入力処理
 */
static int touchin_Change(CB_MAIN_WORK *cbmw)
{
	int ret;

	ret = touchin_Common(cbmw);

	if(ret < 0){
		return CBS_CHANGE_MAIN;
	}
	return input_ChangeCommon(cbmw,ret);
}

/**
 *	@brief	選択画面カーソル移動
 */
static BOOL	keyin_CursorMove(int* cur)
{
	int* sel = cur;

	if (sys.repeat & PAD_KEY_RIGHT){
		(*sel)++;
		(*sel) %= CB_DEF_CORE_MAX;
	}
	else if (sys.repeat & PAD_KEY_LEFT){
		if (*sel > 0){
			(*sel)--;
		}
		else {
			*sel = CB_DEF_CORE_MAX - 1;
		}
	}
	else if (sys.repeat & PAD_KEY_UP){
		if (CB_DEF_CUR_MOVE_UD(*sel) != 0){
			(*sel) -= CB_DEF_LINE;
			(*sel) %= CB_DEF_CORE_MAX;
		}
		else {
			return FALSE;
		}
	}
	else if (sys.repeat & PAD_KEY_DOWN){
		if (CB_DEF_CUR_MOVE_UD(*sel) != (CB_DEF_ROW - 1)){
			(*sel) += CB_DEF_LINE;
			(*sel) %= CB_DEF_CORE_MAX;
		}
		else {
			return FALSE;
		}
	}
	else {
		return FALSE;
	}

	return TRUE;
}

/**
 *	@brief	カーソル移動処理
 */
static void keyin_CursorMovePosUpdate(CB_MAIN_WORK* cbmw, int no, BOOL sub_auto)
{
	s16 x, y;
	int sub = 0;

	if (no == 0){
		sub = 1;
	}

	CB_SelectThisPosGet(cbmw->select[no], &x, &y);
	CATS_ObjectPosSetCap(cbmw->cap_cur[no], x, y);

	if (sub_auto == TRUE){
		cbmw->select[sub] = cbmw->select[no];
		CB_SelectThisPosGet(cbmw->select[sub], &x, &y);
		CATS_ObjectPosSetCap(cbmw->cap_cur[sub], x, y);
	}
}

#define TP_BALL_R	(24)
#define TP_BALL_PX	(40)
#define TP_BALL_PY	(24)
#define TP_BALL_OX	(56)
#define TP_BALL_OY	(56)

#define TP_EXIT_PX	(24*8)
#define TP_DEC_PX	(16*8)
#define TP_BTN_PY	(21*8)
#define TP_BTN_SX	(8*8)
#define TP_BTN_SY	(3*8)

/**
 *	@brief	選択/入れ替え画面タッチ入力処理コモン
 */
static int touchin_Common(CB_MAIN_WORK *cbmw)
{
	int i,j,ret;
	u16 pat;
	TP_HIT_TBL	tbl;
	static const RECT_HIT_TBL Btn_TpRect[] = {
//		{0,191,0,255}, ty,by,lx,rx
		{TP_BTN_PY,TP_BTN_PY+TP_BTN_SY,TP_EXIT_PX,TP_EXIT_PX+TP_BTN_SX},
//	{TP_BTN_PY,TP_BTN_PY+TP_BTN_SY,TP_DEC_PX,TP_DEC_PX+TP_BTN_SX},
		{RECT_HIT_END,0,0,0}
	};

	if(!GF_TP_GetTrg()){
		return -1;
	}
	tbl.circle.code = TP_USE_CIRCLE;
	tbl.circle.r = TP_BALL_R;
	//ボールのあたり判定
	for(i = 0;i < 3;i++){
		tbl.circle.y = TP_BALL_PY+i*TP_BALL_OY;
		for(j = 0;j < 4;j++){
			tbl.circle.x = TP_BALL_PX+j*TP_BALL_OX;
			if(GF_TP_SingleHitTrg((const TP_HIT_TBL*)&tbl)){
				return i*4+j;
			}
		}
	}
	//ボタンのあたり判定
	ret = GF_TP_RectHitTrg(Btn_TpRect);
	if(	ret == RECT_HIT_NONE){
		return -1;
	}
	pat = 0x03FF;	//0000 0000 0111 1100	無効パレットbit(onになっている色Noだけ反応)
	if(GF_BGL_DotCheck(cbmw->sys.bgl,CB_DEF_FIELD_BG_FRM,sys.tp_x,sys.tp_y,&pat) == FALSE){
		return -1;
	}
	return ret+12;
}

/**
 *	@brief	編集画面キー操作
 */
static int Input_Custom(CB_MAIN_WORK* cbmw)
{
	Input_KTStatusChange(cbmw);

	//タッチ操作はボタンマネージャが担当
	if(sys.trg & PAD_BUTTON_DECIDE){
        if(cbmw->sys.hold != CB_DEF_NOT_HOLD){  //シールを持っているのでボタン無効
            Snd_SePlay(CB_SE_BOBOO);
            return CBS_CUSTOM_MAIN;
        }
		CB_Sys_MainTPSet(cbmw, FALSE);
		Snd_SePlay( CB_SE_DEC );
		BA_Init(cbmw->button_obj.cap[CB_ENUM_BHIT_DEC], cbmw->sys.font_obj[0]);
		return CBS_CUSTOM_DEC;
	}else if(sys.trg & PAD_BUTTON_CANCEL){
        if(cbmw->sys.hold != CB_DEF_NOT_HOLD){  //シールを持っているのでボタン無効
            Snd_SePlay(CB_SE_BOBOO);
            return CBS_CUSTOM_MAIN;
        }
		CB_Sys_MainTPSet(cbmw, FALSE);
		Snd_SePlay( CB_SE_CAN );
		BA_Init(cbmw->button_obj.cap[CB_ENUM_BHIT_END], cbmw->sys.font_obj[1]);
		return CBS_CUSTOM_END;
	}else if(sys.trg & (PAD_BUTTON_X|PAD_BUTTON_Y)){
        if(cbmw->sys.hold != CB_DEF_NOT_HOLD){  //シールを持っているのでボタン無効
            Snd_SePlay(CB_SE_BOBOO);
            return CBS_CUSTOM_MAIN;
        }
		Snd_SePlay( CB_SE_PLAY );
		BA_Init(cbmw->button_obj.cap[CB_ENUM_BHIT_CON], NULL);
		return CBS_CUSTOM_CON;
	}
	return CBS_CUSTOM_MAIN;
}


////////////////////////////////////////////////////////////////////////////////////////////
///メニュー
static int CB_MENU_Edit(CB_MAIN_WORK* cbmw)
{
	CB_Sub_SeqChange(cbmw, CB_MODE_EDIT, 0, CBS_SELECT_MAIN);

	return CB_ENUM_MENU_FUNC_RET_NONE;
}

static int CB_MENU_Cancel(CB_MAIN_WORK* cbmw)
{
#if 0
	BmpMenuWinClear(&cbmw->sys.win[ 1 ], WINDOW_TRANS_OFF );
	GF_BGL_BmpWinOff(&cbmw->sys.win[ 1 ]);
	BmpMenuExit(cbmw->sys.bmw, NULL);
	BMP_MENULIST_Delete(cbmw->sys.bml);
	GF_BGL_BmpWinDel(&cbmw->sys.win[ 1 ]);
#else
	CB_BMP_SelectWinDel(cbmw);
	CBDisp_MainBtnSet(cbmw,TRUE);
#endif
	return CB_ENUM_MENU_FUNC_RET_SEQ_PREV;
}

static int CB_MENU_Set(CB_MAIN_WORK* cbmw)
{
	CBS_SVSort(cbmw);
	CBS_EditData_Save(cbmw);
	SubSelect_ObjRefrect(cbmw);

//	CB_Sub_SeqChange(cbmw, CB_MODE_SETTING_SET, 0, 0);
	CBProc_NextModeSet(cbmw->cb_pw, CB_PROC_NEXT_MODE_LIST);
	cbmw->seq = CBS_SELECT_WIPE_OUT;

	return CB_ENUM_MENU_FUNC_RET_NONE;
}

static int CB_MENU_Remove(CB_MAIN_WORK* cbmw)
{
	CB_Sub_SeqChange(cbmw, CB_MODE_SETTING_REMOVE, 0, CBS_SELECT_MAIN);

	return CB_ENUM_MENU_FUNC_RET_NONE;
}

static int CB_MENU_Change(CB_MAIN_WORK* cbmw)
{
	CB_Sub_SeqChange(cbmw, CB_MODE_CHANGE, 0, CBS_SELECT_MAIN);

	return CB_ENUM_MENU_FUNC_RET_NONE;
}


///////////////////////////////////////////////////////////////////////////////////////////
///バックアップ
static void BackupEditData_Make(CB_MAIN_WORK* cbmw)
{
	CB_Tool_CoreData_Copy(cbmw->cb_edit_data[ cbmw->select[0] ].core, &cbmw->backup_edit);
}

static void BackupEditData_Recover(CB_MAIN_WORK* cbmw)
{
	CB_Tool_CoreData_Copy(&cbmw->backup_edit, cbmw->cb_edit_data[ cbmw->select[0] ].core);
}

static void BackupItemData_Make(CB_MAIN_WORK* cbmw)
{
	int i;

	for (i = 0; i < CB_DEF_ITEM_DATA_NUM; i++){
		cbmw->backup_item[ i ] = CB_SaveData_ItemNumGet_CBO(cbmw->cb_item_data, i);
	}
}

static void BackupItemData_Recover(CB_MAIN_WORK* cbmw)
{
	int i;

	for (i = 0; i < CB_DEF_ITEM_DATA_NUM; i++){
		CB_SaveData_ItemNumSet(cbmw->cb_item_data, i, cbmw->backup_item[ i ]);
	}
}

/**
 *	@brief	デモ用ポケモンパラメータ設定
 */
static void CBT_TestPokemonCreate(CB_MAIN_WORK* cbmw)
{
	int i;
	int ret;
	POKEMON_PARAM* pp;

	if (cbmw->cb_edit_data[ cbmw->select[0] ].poke_idx != CB_DEF_POKE_NOT_INDEX){

		pp = cbmw->cb_pw->poke_para[ cbmw->cb_edit_data[ cbmw->select[0] ].poke_idx ];

		PokeCopyPPtoPP(pp, cbmw->test_pp);

		return;
	}

	for (i = 0; i < cbmw->cb_pw->poke_cnt; i++){

		pp = cbmw->cb_pw->poke_para[ i ];

		ret = PokeParaGet(pp, ID_PARA_tamago_flag, NULL);

		if (ret != 0){ continue; }

		PokeCopyPPtoPP(pp, cbmw->test_pp);

		break;
	}
}

static void CB_Sub_SeqChange(CB_MAIN_WORK* cbmw, int mode, int seq, int keep_seq)
{
	if (seq == CB_DEF_KEEP_SEQ_SET){
		cbmw->seq	= cbmw->keep_seq;
	}
	else {
		cbmw->seq	= seq;
	}
	cbmw->mode		= mode;
	cbmw->keep_seq	= keep_seq;
	cbmw->sub_seq	= 0;
}


/**
 *	@brief	ボタンオブジェクト　アニメ制御
 */
static void btnobj_Anime(CATS_ACT_PTR cap, int event)
{
	switch(event){
	case BMN_EVENT_TOUCH:
		CATS_ObjectAnimeFrameSetCap(cap, 1);
		break;
	case BMN_EVENT_HOLD:
		CATS_ObjectAnimeFrameSetCap(cap, 2);
		break;
	case BMN_EVENT_RELEASE:
	case BMN_EVENT_SLIDEOUT:
		CATS_ObjectAnimeFrameSetCap(cap, 0);
		break;
	default:
		GF_ASSERT(0);
		break;
	}
}

/**
 *	@breif	フォントOAM 位置セット
 */
static void FontOamPosSet(FONTOAM_OBJ_PTR obj, int ox, int oy)
{
	int x;
	int y;
	if (obj != NULL){
		FONTOAM_GetMat(obj, &x, &y);
		FONTOAM_SetMat(obj,  x + ox,  y + oy);
	}
}

/*
 *	@brief	ボタンアニメ用　TCB
 */
static void BA_Tcb(TCB_PTR tcb, void* work)
{
	TBUTTON_ANIME* wk = work;

	switch(wk->wait){
	case 3:
		FontOamPosSet(wk->font_obj, 0, -1);
		CATS_ObjectAnimeFrameSetCap(wk->cap, 2);
		wk->wait++;
		break;
	case 6:
		FontOamPosSet(wk->font_obj, 0, +2);
		CATS_ObjectAnimeFrameSetCap(wk->cap, 0);
		TCB_Delete(tcb);
		sys_FreeMemoryEz(wk);
		break;
	default:
		wk->wait++;
		break;
	};
}

/**
 *	@brief	ボタンアニメ初期化
 */
static void BA_Init(CATS_ACT_PTR cap, FONTOAM_OBJ_PTR font_obj)
{
	TBUTTON_ANIME* wk;

	wk = sys_AllocMemory(HEAPID_CUSTOM_BALL, sizeof(TBUTTON_ANIME));

	wk->wait	 = 1;
	wk->cap		 = cap;
	wk->font_obj = font_obj;

	FontOamPosSet(wk->font_obj, 0, -1);

	TCB_Add(BA_Tcb, wk, 1000);
}


/*
 *	@brief	TCBが稼動しているかどうか？
 */
static BOOL CB_Sys_IsSubTCB(CB_MAIN_WORK* cbmw)
{
	return cbmw->sys.sub_tcb_active;
}

/*
 *	@brief	タッチパネルがアクティブかどうか？
 */
static void CB_Sys_MainTPSet(CB_MAIN_WORK* cbmw, BOOL active)
{
	cbmw->sys.main_tp_active = active;
}



static void CB_Test_PokemonSSP_Add(CB_MAIN_WORK* cbmw)
{

	SOFT_SPRITE_ARC ssa;
	SOFT_SPRITE_ANIME	ssanm[SS_ANM_SEQ_MAX];

	int monsno;
	int height;

	PokeGraArcDataGetPP(&ssa, cbmw->test_pp, PARA_FRONT);

	monsno  = PokeParaGet(cbmw->test_pp, ID_PARA_monsno, NULL);
	height	= PokeParaHeightGet(cbmw->test_pp, PARA_FRONT);

	cbmw->sys.height = height;

//	PokeAnmDataSet(&ssanm[0], monsno);
	PokeEditData_AnimeSet( cbmw->handle, &ssanm[0], monsno, CLIENT_TYPE_BB );


	cbmw->sys.ssp = SoftSpriteAdd(cbmw->sys.ssm_p, &ssa,
							   APPEAR_X_TYPE_BB,
							   APPEAR_Y_TYPE_BB + height,
							   APPEAR_Z_TYPE_BB,
							   0, &ssanm[0], NULL);
}

static void CB_Test_PokemonSSP_AnimeInit(CB_MAIN_WORK* cbmw)
{
	int monsno;
	int chr;

	monsno = PokeParaGet(cbmw->test_pp, ID_PARA_monsno, NULL);
	chr	   = PokeSeikakuGet(cbmw->test_pp);

	SoftSpriteAnimeSet(cbmw->sys.ssp, 1);

	PokeEditData_PrgAnimeSet( cbmw->handle,
							  cbmw->sys.pas,
							  cbmw->sys.ssp,
							  monsno,
							  PARA_FRONT,
							  PARA_HFLIP_OFF,
							  0 );
}

static void CB_Test_PokemonSSP_AppearInit(CB_MAIN_WORK* cbmw)
{
	SoftSpriteParaSet(cbmw->sys.ssp, SS_PARA_AFF_X, AFF_APPEAR_X_S);
	SoftSpriteParaSet(cbmw->sys.ssp, SS_PARA_AFF_Y, AFF_APPEAR_Y_S);
}

#define	AFF_SPEED		(0x20)
static BOOL CB_Test_PokemonSSP_AppearMain(CB_MAIN_WORK* cbmw)
{
	if(SoftSpriteParaGet(cbmw->sys.ssp, SS_PARA_AFF_X) == AFF_APPEAR_X_B){
		return FALSE;
	}
	else if(SoftSpriteParaGet(cbmw->sys.ssp, SS_PARA_AFF_X) >= AFF_APPEAR_X_B){
		SoftSpriteParaSet(cbmw->sys.ssp, SS_PARA_AFF_X, AFF_APPEAR_X_B);
		SoftSpriteParaSet(cbmw->sys.ssp, SS_PARA_AFF_Y, AFF_APPEAR_Y_B);
		return FALSE;
	}
	else {
		SoftSpriteParaCalc(cbmw->sys.ssp, SS_PARA_AFF_X, AFF_SPEED);
		SoftSpriteParaCalc(cbmw->sys.ssp, SS_PARA_AFF_Y, AFF_SPEED);
		SoftSpriteOYCalc(cbmw->sys.ssp, cbmw->sys.height);
	}
	return TRUE;
}

static BOOL CB_Test_PokemonSSP_ReturnMain(CB_MAIN_WORK* cbmw)
{
	if(SoftSpriteParaGet(cbmw->sys.ssp, SS_PARA_AFF_X) == AFF_APPEAR_X_S){
		return FALSE;
	}
	else if(SoftSpriteParaGet(cbmw->sys.ssp, SS_PARA_AFF_X) <= AFF_APPEAR_X_S){
		SoftSpriteParaSet(cbmw->sys.ssp, SS_PARA_AFF_X, AFF_APPEAR_X_S);
		SoftSpriteParaSet(cbmw->sys.ssp, SS_PARA_AFF_Y, AFF_APPEAR_Y_S);
		return FALSE;
	}
	else {
		SoftSpriteParaCalc(cbmw->sys.ssp, SS_PARA_AFF_X, -AFF_SPEED);
		SoftSpriteParaCalc(cbmw->sys.ssp, SS_PARA_AFF_Y, -AFF_SPEED);
		SoftSpriteOYCalc(cbmw->sys.ssp, cbmw->sys.height);
	}
	return TRUE;
}

static void CB_Test_PokemonSSP_Vanish(CB_MAIN_WORK* cbmw, int flag)
{
	SoftSpriteParaSet(cbmw->sys.ssp, SS_PARA_VANISH, flag);
}

static void CB_Test_PokemonSSP_Del(CB_MAIN_WORK* cbmw)
{
	SoftSpriteDel(cbmw->sys.ssp);
}